home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcl / sptmbr16.lha / cache.lisp < prev    next >
Lisp/Scheme  |  1992-12-21  |  52KB  |  1,485 lines

  1. ;;;-*-Mode:LISP; Package:(PCL LISP 1000); Base:10; Syntax:Common-lisp -*-
  2. ;;;
  3. ;;; *************************************************************************
  4. ;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
  5. ;;; All rights reserved.
  6. ;;;
  7. ;;; Use and copying of this software and preparation of derivative works
  8. ;;; based upon this software are permitted.  Any distribution of this
  9. ;;; software or derivative works must comply with all applicable United
  10. ;;; States export control laws.
  11. ;;; 
  12. ;;; This software is made available AS IS, and Xerox Corporation makes no
  13. ;;; warranty about the software, its performance or its conformity to any
  14. ;;; specification.
  15. ;;; 
  16. ;;; Any person obtaining a copy of this software is requested to send their
  17. ;;; name and post office or electronic mail address to:
  18. ;;;   CommonLoops Coordinator
  19. ;;;   Xerox PARC
  20. ;;;   3333 Coyote Hill Rd.
  21. ;;;   Palo Alto, CA 94304
  22. ;;; (or send Arpanet mail to CommonLoops-Coordinator.pa@Xerox.arpa)
  23. ;;;
  24. ;;; Suggestions, comments and requests for improvements are also welcome.
  25. ;;; *************************************************************************
  26. ;;;
  27. ;;; The basics of the PCL wrapper cache mechanism.
  28. ;;;
  29.  
  30. (in-package :pcl)
  31. ;;;
  32. ;;; The caching algorithm implemented:
  33. ;;;
  34. ;;; << put a paper here >>
  35. ;;;
  36. ;;; For now, understand that as far as most of this code goes, a cache has
  37. ;;; two important properties.  The first is the number of wrappers used as
  38. ;;; keys in each cache line.  Throughout this code, this value is always
  39. ;;; called NKEYS.  The second is whether or not the cache lines of a cache
  40. ;;; store a value.  Throughout this code, this always called VALUEP.
  41. ;;;
  42. ;;; Depending on these values, there are three kinds of caches.
  43. ;;;
  44. ;;; NKEYS = 1, VALUEP = NIL
  45. ;;;
  46. ;;; In this kind of cache, each line is 1 word long.  No cache locking is
  47. ;;; needed since all read's in the cache are a single value.  Nevertheless
  48. ;;; line 0 (location 0) is reserved, to ensure that invalid wrappers will
  49. ;;; not get a first probe hit.
  50. ;;;
  51. ;;; To keep the code simpler, a cache lock count does appear in location 0
  52. ;;; of these caches, that count is incremented whenever data is written to
  53. ;;; the cache.  But, the actual lookup code (see make-dlap) doesn't need to
  54. ;;; do locking when reading the cache.
  55. ;;; 
  56. ;;;
  57. ;;; NKEYS = 1, VALUEP = T
  58. ;;;
  59. ;;; In this kind of cache, each line is 2 words long.  Cache locking must
  60. ;;; be done to ensure the synchronization of cache reads.  Line 0 of the
  61. ;;; cache (location 0) is reserved for the cache lock count.  Location 1
  62. ;;; of the cache is unused (in effect wasted).
  63. ;;; 
  64. ;;; NKEYS > 1
  65. ;;;
  66. ;;; In this kind of cache, the 0 word of the cache holds the lock count.
  67. ;;; The 1 word of the cache is line 0.  Line 0 of these caches is not
  68. ;;; reserved.
  69. ;;;
  70. ;;; This is done because in this sort of cache, the overhead of doing the
  71. ;;; cache probe is high enough that the 1+ required to offset the location
  72. ;;; is not a significant cost.  In addition, because of the larger line
  73. ;;; sizes, the space that would be wasted by reserving line 0 to hold the
  74. ;;; lock count is more significant.
  75. ;;;
  76.  
  77. ;;;
  78. ;;; Caches
  79. ;;;
  80. ;;; A cache is essentially just a vector.  The use of the individual `words'
  81. ;;; in the vector depends on particular properties of the cache as described
  82. ;;; above.
  83. ;;;
  84. ;;; This defines an abstraction for caches in terms of their most obvious
  85. ;;; implementation as simple vectors.  But, please notice that part of the
  86. ;;; implementation of this abstraction, is the function lap-out-cache-ref.
  87. ;;; This means that most port-specific modifications to the implementation
  88. ;;; of caches will require corresponding port-specific modifications to the
  89. ;;; lap code assembler.
  90. ;;;
  91. (defmacro cache-vector-ref (cache-vector location)
  92.   `(svref (the simple-vector ,cache-vector)
  93.           (#-cmu the #+cmu ext:truly-the fixnum ,location)))
  94.  
  95. (defmacro cache-vector-size (cache-vector)
  96.   `(array-dimension (the simple-vector ,cache-vector) 0))
  97.  
  98. (defun allocate-cache-vector (size)
  99.   (make-array size :adjustable nil))
  100.  
  101. (defmacro cache-vector-lock-count (cache-vector)
  102.   `(cache-vector-ref ,cache-vector 0))
  103.  
  104. (defun flush-cache-vector-internal (cache-vector)
  105.   (without-interrupts  
  106.     (fill (the simple-vector cache-vector) nil)
  107.     (setf (cache-vector-lock-count cache-vector) 0))
  108.   cache-vector)
  109.  
  110. (defmacro modify-cache (cache-vector &body body)
  111.   `(without-interrupts
  112.      (multiple-value-prog1
  113.        (progn ,@body)
  114.        (let ((old-count (cache-vector-lock-count ,cache-vector)))
  115.      (declare (fixnum old-count))
  116.      (setf (cache-vector-lock-count ,cache-vector)
  117.            (if (= old-count most-positive-fixnum)
  118.            1 (the fixnum (1+ old-count))))))))
  119.  
  120. (deftype field-type ()
  121.   '(integer 0    ;#.(position 'number wrapper-layout)
  122.             7))  ;#.(position 'number wrapper-layout :from-end t)
  123.  
  124. (eval-when (compile load eval)
  125. (defun power-of-two-ceiling (x)
  126.   (declare (fixnum x))
  127.   ;;(expt 2 (ceiling (log x 2)))
  128.   (the fixnum (ash 1 (integer-length (1- x)))))
  129.  
  130. (defconstant *nkeys-limit* 256)
  131. )
  132.  
  133. (defstruct (cache
  134.          (:print-function print-cache)
  135.          (:constructor make-cache ())
  136.          (:copier copy-cache-internal))
  137.   (owner nil)
  138.   (nkeys 1 :type (integer 1 #.*nkeys-limit*))
  139.   (valuep nil :type (member nil t))
  140.   (nlines 0 :type fixnum)
  141.   (field 0 :type field-type)
  142.   (limit-fn #'default-limit-fn :type function)
  143.   (mask 0 :type fixnum)
  144.   (size 0 :type fixnum)
  145.   (line-size 1 :type (integer 1 #.(power-of-two-ceiling (1+ *nkeys-limit*))))
  146.   (max-location 0 :type fixnum)
  147.   (vector #() :type simple-vector)
  148.   (overflow nil :type list))
  149.  
  150. (defun print-cache (cache stream depth)
  151.   (declare (ignore depth))
  152.   (printing-random-thing (cache stream)
  153.     (format stream "cache ~D ~S ~D" 
  154.         (cache-nkeys cache) (cache-valuep cache) (cache-nlines cache))))
  155.  
  156. #+akcl
  157. (si::freeze-defstruct 'cache)
  158.  
  159. (defmacro cache-lock-count (cache)
  160.   `(cache-vector-lock-count (cache-vector ,cache)))
  161.  
  162.  
  163. ;;;
  164. ;;; Some facilities for allocation and freeing caches as they are needed.
  165. ;;; This is done on the assumption that a better port of PCL will arrange
  166. ;;; to cons these all the same static area.  Given that, the fact that
  167. ;;; PCL tries to reuse them should be a win.
  168. ;;; 
  169. (defvar *free-cache-vectors* (make-hash-table :size 16 :test 'eql))
  170.  
  171. ;;;
  172. ;;; Return a cache that has had flush-cache-vector-internal called on it.  This
  173. ;;; returns a cache of exactly the size requested, it won't ever return a
  174. ;;; larger cache.
  175. ;;; 
  176. (defun get-cache-vector (size)
  177.   (let ((entry (gethash size *free-cache-vectors*)))
  178.     (without-interrupts
  179.       (cond ((null entry)
  180.          (setf (gethash size *free-cache-vectors*) (cons 0 nil))
  181.          (get-cache-vector size))
  182.         ((null (cdr entry))
  183.          (incf (car entry))
  184.          (flush-cache-vector-internal (allocate-cache-vector size)))
  185.         (t
  186.          (let ((cache (cdr entry)))
  187.            (setf (cdr entry) (cache-vector-ref cache 0))
  188.            (flush-cache-vector-internal cache)))))))
  189.  
  190. (defun free-cache-vector (cache-vector)
  191.   (let ((entry (gethash (cache-vector-size cache-vector) *free-cache-vectors*)))
  192.     (without-interrupts
  193.       (if (null entry)
  194.       (error "Attempt to free a cache-vector not allocated by GET-CACHE-VECTOR.")
  195.       (let ((thread (cdr entry)))
  196.         (loop (unless thread (return))
  197.           (when (eq thread cache-vector) (error "Freeing a cache twice."))
  198.           (setq thread (cache-vector-ref thread 0)))      
  199.         (flush-cache-vector-internal cache-vector)        ;Help the GC
  200.         (setf (cache-vector-ref cache-vector 0) (cdr entry))
  201.         (setf (cdr entry) cache-vector)
  202.         nil)))))
  203.  
  204. ;;;
  205. ;;; This is just for debugging and analysis.  It shows the state of the free
  206. ;;; cache resource.
  207. ;;; 
  208. (defun show-free-cache-vectors ()
  209.   (let ((elements ()))
  210.     (maphash #'(lambda (s e) (push (list s e) elements)) *free-cache-vectors*)
  211.     (setq elements (sort elements #'< :key #'car))
  212.     (dolist (e elements)
  213.       (let* ((size (car e))
  214.          (entry (cadr e))
  215.          (allocated (car entry))
  216.          (head (cdr entry))
  217.          (free 0))
  218.     (loop (when (null head) (return t))
  219.           (setq head (cache-vector-ref head 0))
  220.           (incf free))
  221.     (format t
  222.         "~&There  ~4D are caches of size ~4D. (~D free  ~3D%)"
  223.         allocated
  224.         size
  225.         free
  226.         (floor (* 100 (/ free (float allocated)))))))))
  227.  
  228.  
  229. ;;;
  230. ;;; Wrapper cache numbers
  231. ;;; 
  232.  
  233. ;;;
  234. ;;; The constant WRAPPER-CACHE-NUMBER-ADDS-OK controls the number of non-zero
  235. ;;; bits wrapper cache numbers will have.
  236. ;;;
  237. ;;; The value of this constant is the number of wrapper cache numbers which
  238. ;;; can be added and still be certain the result will be a fixnum.  This is
  239. ;;; used by all the code that computes primary cache locations from multiple
  240. ;;; wrappers.
  241. ;;;
  242. ;;; The value of this constant is used to derive the next two which are the
  243. ;;; forms of this constant which it is more convenient for the runtime code
  244. ;;; to use.
  245. ;;; 
  246. (eval-when (compile load eval)
  247.  
  248. (defconstant wrapper-cache-number-adds-ok 4)
  249.  
  250. (defconstant wrapper-cache-number-length
  251.          (- (integer-length most-positive-fixnum)
  252.         wrapper-cache-number-adds-ok))
  253.  
  254. (defconstant wrapper-cache-number-mask
  255.          (1- (expt 2 wrapper-cache-number-length)))
  256.  
  257.  
  258. (defvar *get-wrapper-cache-number* (make-random-state))
  259.  
  260. (defun get-wrapper-cache-number ()
  261.   (let ((n 0))
  262.     (declare (fixnum n))
  263.     (loop
  264.       (setq n
  265.         (logand wrapper-cache-number-mask
  266.             (random most-positive-fixnum *get-wrapper-cache-number*)))
  267.       (unless (zerop n) (return n)))))
  268.  
  269.  
  270. (unless (> wrapper-cache-number-length 8)
  271.   (error "In this implementation of Common Lisp, fixnums are so small that~@
  272.           wrapper cache numbers end up being only ~D bits long.  This does~@
  273.           not actually keep PCL from running, but it may degrade cache~@
  274.           performance.~@
  275.           You may want to consider changing the value of the constant~@
  276.           WRAPPER-CACHE-NUMBER-ADDS-OK.")))
  277.  
  278.  
  279. ;;;
  280. ;;; wrappers themselves
  281. ;;;
  282. ;;; This caching algorithm requires that wrappers have more than one wrapper
  283. ;;; cache number.  You should think of these multiple numbers as being in
  284. ;;; columns.  That is, for a given cache, the same column of wrapper cache
  285. ;;; numbers will be used.
  286. ;;;
  287. ;;; If at some point the cache distribution of a cache gets bad, the cache
  288. ;;; can be rehashed by switching to a different column.
  289. ;;;
  290. ;;; The columns are referred to by field number which is that number which,
  291. ;;; when used as a second argument to wrapper-ref, will return that column
  292. ;;; of wrapper cache number.
  293. ;;;
  294. ;;; This code is written to allow flexibility as to how many wrapper cache
  295. ;;; numbers will be in each wrapper, and where they will be located.  It is
  296. ;;; also set up to allow port specific modifications to `pack' the wrapper
  297. ;;; cache numbers on machines where the addressing modes make that a good
  298. ;;; idea.
  299. ;;; 
  300. #-structure-wrapper
  301. (progn
  302. (eval-when (compile load eval)
  303. (defconstant wrapper-layout
  304.          '(number
  305.            number
  306.            number
  307.            number
  308.            number
  309.            number
  310.            number
  311.            number
  312.            state
  313.            instance-slots-layout
  314.            class-slots
  315.            class
  316.            no-of-instance-slots))
  317. )
  318.  
  319. (eval-when (compile load eval)
  320.  
  321. (defun wrapper-field (type)
  322.   (posq type wrapper-layout))
  323.  
  324. (defun next-wrapper-field (field-number)
  325.   (position (nth field-number wrapper-layout)
  326.         wrapper-layout
  327.         :start (1+ field-number)))
  328.  
  329. (defmacro first-wrapper-cache-number-index ()
  330.   `(wrapper-field 'number))
  331.  
  332. (defmacro next-wrapper-cache-number-index (field-number)
  333.   `(next-wrapper-field ,field-number))
  334.  
  335. );eval-when
  336.  
  337. (defmacro wrapper-cache-number-vector (wrapper)
  338.   wrapper)
  339.  
  340. (defmacro cache-number-vector-ref (cnv n)
  341.   `(svref ,cnv ,n))
  342.  
  343.  
  344. (defmacro wrapper-ref (wrapper n)
  345.   `(svref ,wrapper ,n))
  346.  
  347. (defmacro wrapper-state (wrapper)
  348.   `(wrapper-ref ,wrapper ,(wrapper-field 'state)))
  349.  
  350. (defmacro wrapper-instance-slots-layout (wrapper)
  351.   `(wrapper-ref ,wrapper ,(wrapper-field 'instance-slots-layout)))
  352.  
  353. (defmacro wrapper-class-slots (wrapper)
  354.   `(wrapper-ref ,wrapper ,(wrapper-field 'class-slots)))
  355.  
  356. (defmacro wrapper-class (wrapper)
  357.   `(wrapper-ref ,wrapper ,(wrapper-field 'class)))
  358.  
  359. (defmacro wrapper-no-of-instance-slots (wrapper)
  360.   `(wrapper-ref ,wrapper ,(wrapper-field 'no-of-instance-slots)))
  361.  
  362. (defmacro make-wrapper-internal ()
  363.   `(let ((wrapper (make-array ,(length wrapper-layout) :adjustable nil)))
  364.      ,@(gathering1 (collecting)
  365.      (iterate ((i (interval :from 0))
  366.            (desc (list-elements wrapper-layout)))
  367.        (ecase desc
  368.          (number
  369.           (gather1 `(setf (wrapper-ref wrapper ,i)
  370.                   (get-wrapper-cache-number))))
  371.          ((state instance-slots-layout class-slots class no-of-instance-slots)))))
  372.      (setf (wrapper-state wrapper) 't)     
  373.      wrapper))
  374.  
  375. (defun make-wrapper (no-of-instance-slots &optional class)
  376.   (let ((wrapper (make-wrapper-internal)))
  377.     (setf (wrapper-no-of-instance-slots wrapper) no-of-instance-slots)
  378.     (setf (wrapper-class wrapper) class)
  379.     wrapper))
  380.  
  381. )
  382.  
  383. ; In CMUCL we want to do type checking as early as possible; structures help this.
  384. #+structure-wrapper
  385. (eval-when (compile load eval)
  386.  
  387. (defconstant wrapper-cache-number-vector-length 8)
  388.  
  389. (deftype cache-number-vector ()
  390.   `(simple-array fixnum (8)))
  391.  
  392. (defconstant wrapper-layout (make-list wrapper-cache-number-vector-length
  393.                        :initial-element 'number))
  394.  
  395. )
  396.  
  397. #+structure-wrapper
  398. (progn
  399.  
  400. #-new-kcl-wrapper
  401. (defun make-wrapper-cache-number-vector ()
  402.   (let ((cnv (make-array #.wrapper-cache-number-vector-length
  403.              :element-type 'fixnum)))
  404.     (dotimes (i #.wrapper-cache-number-vector-length)
  405.       (setf (aref cnv i) (get-wrapper-cache-number)))
  406.     cnv))
  407.  
  408. (defstruct (wrapper
  409.          #+new-kcl-wrapper (:include si::basic-wrapper)
  410.          (:print-function print-wrapper)
  411.          #-new-kcl-wrapper
  412.          (:constructor make-wrapper (no-of-instance-slots &optional class))
  413.          #+new-kcl-wrapper
  414.          (:constructor make-wrapper-internal))
  415.   #-new-kcl-wrapper
  416.   (cache-number-vector (make-wrapper-cache-number-vector)
  417.                :type cache-number-vector)
  418.   #-new-kcl-wrapper
  419.   (state t :type (or (member t) cons)) 
  420.   ;;  either t or a list (state-sym new-wrapper)
  421.   ;;           where state-sym is either :flush or :obsolete
  422.   (instance-slots-layout nil :type list)
  423.   (class-slots nil :type list)
  424.   #-new-kcl-wrapper
  425.   (no-of-instance-slots 0 :type fixnum)
  426.   #-new-kcl-wrapper
  427.   (class *the-class-t* :type class))
  428.  
  429. (unless (boundp '*the-class-t*) (setq *the-class-t* nil))
  430.  
  431. #+new-kcl-wrapper
  432. (defmacro wrapper-no-of-instance-slots (wrapper)
  433.   `(si::s-data-length ,wrapper))
  434.  
  435. #+new-kcl-wrapper
  436. (defun make-wrapper (size &optional class)
  437.   (multiple-value-bind (raw slot-positions)
  438.       (if (< size 50)
  439.       (values si::*all-t-s-type* si::*standard-slot-positions*)
  440.       (values (make-array size :element-type 'unsigned-char)
  441.           (let ((array (make-array size :element-type 'unsigned-short)))
  442.             (dotimes (i size)
  443.               (declare (fixnum i))
  444.               (setf (aref array i) (* #.(si::size-of t) i))))))
  445.     (make-wrapper-internal :length size
  446.                :raw raw
  447.                :print-function 'print-std-instance
  448.                :slot-position slot-positions
  449.                :size (* size #.(si::size-of t))
  450.                :class class)))
  451.  
  452. (defun print-wrapper (wrapper stream depth)
  453.   (declare (ignore depth))
  454.   (printing-random-thing (wrapper stream)
  455.     (format stream "Wrapper ~S" (wrapper-class wrapper))))
  456.  
  457. (defmacro first-wrapper-cache-number-index ()
  458.   0)
  459.  
  460. (defmacro next-wrapper-cache-number-index (field-number)
  461.   `(and (< ,field-number #.(1- wrapper-cache-number-vector-length))
  462.         (1+ ,field-number)))
  463.  
  464. (defmacro cache-number-vector-ref (cnv n)
  465.   `(#-kcl svref #+kcl aref ,cnv ,n))
  466.  
  467. )
  468.  
  469. (defmacro wrapper-cache-number-vector-ref (wrapper n)
  470.   `(the fixnum
  471.         (#-structure-wrapper svref #+structure-wrapper aref
  472.           (wrapper-cache-number-vector ,wrapper) ,n)))
  473.  
  474. (defmacro class-no-of-instance-slots (class)
  475.   `(wrapper-no-of-instance-slots (class-wrapper ,class)))
  476.  
  477. (defmacro wrapper-class* (wrapper)
  478.   #-new-kcl-wrapper
  479.   `(wrapper-class ,wrapper)
  480.   #+new-kcl-wrapper
  481.   `(let ((wrapper ,wrapper))
  482.      (or (wrapper-class wrapper)
  483.          (find-structure-class (si::s-data-name wrapper)))))
  484.  
  485. ;;;
  486. ;;; The wrapper cache machinery provides general mechanism for trapping on
  487. ;;; the next access to any instance of a given class.  This mechanism is
  488. ;;; used to implement the updating of instances when the class is redefined
  489. ;;; (make-instances-obsolete).  The same mechanism is also used to update
  490. ;;; generic function caches when there is a change to the supers of a class.
  491. ;;;
  492. ;;; Basically, a given wrapper can be valid or invalid.  If it is invalid,
  493. ;;; it means that any attempt to do a wrapper cache lookup using the wrapper
  494. ;;; should trap.  Also, methods on slot-value-using-class check the wrapper
  495. ;;; validity as well.  This is done by calling check-wrapper-validity.
  496. ;;; 
  497.  
  498. (defmacro invalid-wrapper-p (wrapper)
  499.   `(neq (wrapper-state ,wrapper) 't))
  500.  
  501. (defvar *previous-nwrappers* (make-hash-table))
  502.  
  503. (defun invalidate-wrapper (owrapper state nwrapper)
  504.   (ecase state
  505.     ((:flush :obsolete)
  506.      (let ((new-previous ()))
  507.        ;;
  508.        ;; First off, a previous call to invalidate-wrapper may have recorded
  509.        ;; owrapper as an nwrapper to update to.  Since owrapper is about to
  510.        ;; be invalid, it no longer makes sense to update to it.
  511.        ;;
  512.        ;; We go back and change the previously invalidated wrappers so that
  513.        ;; they will now update directly to nwrapper.  This corresponds to a
  514.        ;; kind of transitivity of wrapper updates.
  515.        ;; 
  516.        (dolist (previous (gethash owrapper *previous-nwrappers*))
  517.      (when (eq state ':obsolete)
  518.        (setf (car previous) ':obsolete))
  519.      (setf (cadr previous) nwrapper)
  520.      (push previous new-previous))
  521.        
  522.        (let ((ocnv (wrapper-cache-number-vector owrapper)))
  523.      (iterate ((type (list-elements wrapper-layout))
  524.            (i (interval :from 0)))
  525.            (when (eq type 'number) (setf (cache-number-vector-ref ocnv i) 0))))
  526.        (push (setf (wrapper-state owrapper) (list state nwrapper))
  527.          new-previous)
  528.        
  529.        (setf (gethash owrapper *previous-nwrappers*) ()
  530.          (gethash nwrapper *previous-nwrappers*) new-previous)))))
  531.  
  532. (defun check-wrapper-validity (instance)
  533.   (let* ((owrapper (wrapper-of instance))
  534.      (state (wrapper-state owrapper)))
  535.     (if (eq state  't)
  536.     owrapper
  537.     (let ((nwrapper
  538.         (ecase (car state)
  539.           (:flush
  540.             (flush-cache-trap owrapper (cadr state) instance))
  541.           (:obsolete
  542.             (obsolete-instance-trap owrapper (cadr state) instance)))))
  543.       ;;
  544.       ;; This little bit of error checking is superfluous.  It only
  545.       ;; checks to see whether the person who implemented the trap
  546.       ;; handling screwed up.  Since that person is hacking internal
  547.       ;; PCL code, and is not a user, this should be needless.  Also,
  548.       ;; since this directly slows down instance update and generic
  549.       ;; function cache refilling, feel free to take it out sometime
  550.       ;; soon.
  551.       ;; 
  552.       (cond ((neq nwrapper (wrapper-of instance))
  553.          (error "Wrapper returned from trap not wrapper of instance."))
  554.         ((invalid-wrapper-p nwrapper)
  555.          (error "Wrapper returned from trap invalid.")))
  556.       nwrapper))))
  557.  
  558. (defmacro check-wrapper-validity1 (object)
  559.   (let ((owrapper (gensym)))
  560.     `(let ((,owrapper (cond ((std-instance-p ,object)
  561.                  (std-instance-wrapper ,object))
  562.                 ((fsc-instance-p ,object)
  563.                  (fsc-instance-wrapper ,object))
  564.                 #+new-kcl-wrapper
  565.                 (t (built-in-wrapper-of ,object))
  566.                 #-new-kcl-wrapper
  567.                 (t (wrapper-of ,object)))))
  568.        (if (eq 't (wrapper-state ,owrapper))
  569.        ,owrapper
  570.        (check-wrapper-validity ,object)))))
  571.  
  572.  
  573. (defvar *free-caches* nil)
  574.  
  575. (defun get-cache (nkeys valuep limit-fn nlines)
  576.   (let ((cache (or (without-interrupts (pop *free-caches*)) (make-cache))))
  577.     (declare (type cache cache))
  578.     (multiple-value-bind (cache-mask actual-size line-size nlines)
  579.     (compute-cache-parameters nkeys valuep nlines)
  580.       (setf (cache-nkeys cache) nkeys
  581.         (cache-valuep cache) valuep
  582.         (cache-nlines cache) nlines
  583.         (cache-field cache) (first-wrapper-cache-number-index)
  584.         (cache-limit-fn cache) limit-fn
  585.         (cache-mask cache) cache-mask
  586.         (cache-size cache) actual-size
  587.         (cache-line-size cache) line-size
  588.         (cache-max-location cache) (let ((line (1- nlines)))
  589.                      (if (= nkeys 1)
  590.                          (* line line-size)
  591.                          (1+ (* line line-size))))
  592.         (cache-vector cache) (get-cache-vector actual-size)
  593.         (cache-overflow cache) nil)
  594.       cache)))
  595.  
  596. (defun get-cache-from-cache (old-cache new-nlines 
  597.                  &optional (new-field (first-wrapper-cache-number-index)))
  598.   (let ((nkeys (cache-nkeys old-cache))
  599.     (valuep (cache-valuep old-cache))
  600.     (cache (or (without-interrupts (pop *free-caches*)) (make-cache))))
  601.     (declare (type cache cache))
  602.     (multiple-value-bind (cache-mask actual-size line-size nlines)
  603.     (if (= new-nlines (cache-nlines old-cache))
  604.         (values (cache-mask old-cache) (cache-size old-cache) 
  605.             (cache-line-size old-cache) (cache-nlines old-cache))
  606.         (compute-cache-parameters nkeys valuep new-nlines))
  607.       (setf (cache-owner cache) (cache-owner old-cache)
  608.         (cache-nkeys cache) nkeys
  609.         (cache-valuep cache) valuep
  610.         (cache-nlines cache) nlines
  611.         (cache-field cache) new-field
  612.         (cache-limit-fn cache) (cache-limit-fn old-cache)
  613.         (cache-mask cache) cache-mask
  614.         (cache-size cache) actual-size
  615.         (cache-line-size cache) line-size
  616.         (cache-max-location cache) (let ((line (1- nlines)))
  617.                      (if (= nkeys 1)
  618.                          (* line line-size)
  619.                          (1+ (* line line-size))))
  620.         (cache-vector cache) (get-cache-vector actual-size)
  621.         (cache-overflow cache) nil)
  622.       cache)))
  623.  
  624. (defun copy-cache (old-cache)
  625.   (let* ((new-cache (copy-cache-internal old-cache))
  626.      (size (cache-size old-cache))
  627.      (old-vector (cache-vector old-cache))
  628.      (new-vector (get-cache-vector size)))
  629.     (declare (simple-vector old-vector new-vector))
  630.     (dotimes (i size)
  631.       (setf (svref new-vector i) (svref old-vector i)))
  632.     (setf (cache-vector new-cache) new-vector)
  633.     new-cache))
  634.  
  635. (defun free-cache (cache)
  636.   (free-cache-vector (cache-vector cache))
  637.   (setf (cache-vector cache) #())
  638.   (setf (cache-owner cache) nil)
  639.   (push cache *free-caches*)
  640.   nil)
  641.  
  642. (defun compute-line-size (x)
  643.   (power-of-two-ceiling x))
  644.  
  645. (defun compute-cache-parameters (nkeys valuep nlines-or-cache-vector)
  646.   ;;(declare (values cache-mask actual-size line-size nlines))
  647.   (declare (fixnum nkeys))
  648.   (if (= nkeys 1)
  649.       (let* ((line-size (if valuep 2 1))
  650.          (cache-size (if (typep nlines-or-cache-vector 'fixnum)
  651.                  (the fixnum 
  652.                   (* line-size
  653.                      (the fixnum 
  654.                       (power-of-two-ceiling 
  655.                         nlines-or-cache-vector))))
  656.                  (cache-vector-size nlines-or-cache-vector))))
  657.     (declare (fixnum line-size cache-size))
  658.     (values (logxor (the fixnum (1- cache-size)) (the fixnum (1- line-size)))
  659.         cache-size
  660.         line-size
  661.         (the fixnum (floor cache-size line-size))))
  662.       (let* ((line-size (power-of-two-ceiling (if valuep (1+ nkeys) nkeys)))
  663.          (cache-size (if (typep nlines-or-cache-vector 'fixnum)
  664.                  (the fixnum
  665.                   (* line-size
  666.                      (the fixnum
  667.                       (power-of-two-ceiling 
  668.                         nlines-or-cache-vector))))
  669.                  (1- (cache-vector-size nlines-or-cache-vector)))))
  670.     (declare (fixnum line-size cache-size))
  671.     (values (logxor (the fixnum (1- cache-size)) (the fixnum (1- line-size)))
  672.         (the fixnum (1+ cache-size))
  673.         line-size
  674.         (the fixnum (floor cache-size line-size))))))
  675.  
  676.  
  677.  
  678. ;;;
  679. ;;; The various implementations of computing a primary cache location from
  680. ;;; wrappers.  Because some implementations of this must run fast there are
  681. ;;; several implementations of the same algorithm.
  682. ;;;
  683. ;;; The algorithm is:
  684. ;;;
  685. ;;;  SUM       over the wrapper cache numbers,
  686. ;;;  ENSURING  that the result is a fixnum
  687. ;;;  MASK      the result against the mask argument.
  688. ;;;
  689. ;;;
  690.  
  691. ;;;
  692. ;;; COMPUTE-PRIMARY-CACHE-LOCATION
  693. ;;; 
  694. ;;; The basic functional version.  This is used by the cache miss code to
  695. ;;; compute the primary location of an entry.  
  696. ;;;
  697. (defun compute-primary-cache-location (field mask wrappers)
  698.   (declare (type field-type field) (fixnum mask))
  699.   (if (not (listp wrappers))
  700.       (logand mask (the fixnum (wrapper-cache-number-vector-ref wrappers field)))
  701.       (let ((location 0) (i 0))
  702.     (declare (fixnum location i))
  703.     (dolist (wrapper wrappers)
  704.       ;;
  705.       ;; First add the cache number of this wrapper to location.
  706.       ;; 
  707.       (let ((wrapper-cache-number (wrapper-cache-number-vector-ref wrapper field)))
  708.         (declare (fixnum wrapper-cache-number))
  709.         (if (zerop wrapper-cache-number)
  710.         (return-from compute-primary-cache-location 0)
  711.         (setq location (the fixnum (+ location wrapper-cache-number)))))
  712.       ;;
  713.       ;; Then, if we are working with lots of wrappers, deal with
  714.       ;; the wrapper-cache-number-mask stuff.
  715.       ;; 
  716.       (when (and (not (zerop i))
  717.              (zerop (mod i wrapper-cache-number-adds-ok)))
  718.         (setq location
  719.           (logand location wrapper-cache-number-mask)))
  720.       (incf i))
  721.     (the fixnum (1+ (logand mask location))))))
  722.  
  723. ;;;
  724. ;;; COMPUTE-PRIMARY-CACHE-LOCATION-FROM-LOCATION
  725. ;;;
  726. ;;; This version is called on a cache line.  It fetches the wrappers from
  727. ;;; the cache line and determines the primary location.  Various parts of
  728. ;;; the cache filling code call this to determine whether it is appropriate
  729. ;;; to displace a given cache entry.
  730. ;;; 
  731. ;;; If this comes across a wrapper whose cache-no is 0, it returns the symbol
  732. ;;; invalid to suggest to its caller that it would be provident to blow away
  733. ;;; the cache line in question.
  734. ;;;
  735. (defun compute-primary-cache-location-from-location (to-cache from-location 
  736.                              &optional (from-cache to-cache))
  737.   (declare (type cache to-cache from-cache) (fixnum from-location))
  738.   (let ((result 0)
  739.     (cache-vector (cache-vector from-cache))
  740.     (field (cache-field to-cache))
  741.     (mask (cache-mask to-cache))
  742.     (nkeys (cache-nkeys to-cache)))
  743.     (declare (type field-type field) (fixnum result mask nkeys)
  744.          (simple-vector cache-vector))
  745.     (dotimes (i nkeys)
  746.       (let* ((wrapper (cache-vector-ref cache-vector (+ i from-location)))
  747.          (wcn (wrapper-cache-number-vector-ref wrapper field)))
  748.     (declare (fixnum wcn))
  749.     (setq result (+ result wcn)))
  750.       (when (and (not (zerop i))
  751.          (zerop (mod i wrapper-cache-number-adds-ok)))
  752.     (setq result (logand result wrapper-cache-number-mask))))    
  753.     (if (= nkeys 1)
  754.     (logand mask result)
  755.     (the fixnum (1+ (logand mask result))))))
  756.  
  757.  
  758. ;;;
  759. ;;;  NIL              means nothing so far, no actual arg info has NILs
  760. ;;;                   in the metatype
  761. ;;;  CLASS            seen all sorts of metaclasses
  762. ;;;                   (specifically, more than one of the next 4 values)
  763. ;;;  T                means everything so far is the class T
  764. ;;;  STANDARD-CLASS   seen only standard classes
  765. ;;;  BUILT-IN-CLASS   seen only built in classes
  766. ;;;  STRUCTURE-CLASS  seen only structure classes
  767. ;;;  
  768. (defun raise-metatype (metatype new-specializer)
  769.   (let ((slot      (find-class 'slot-class))
  770.     (standard  (find-class 'standard-class))
  771.     (fsc       (find-class 'funcallable-standard-class))
  772.     (structure (find-class 'structure-class))
  773.     (built-in  (find-class 'built-in-class)))
  774.     (flet ((specializer->metatype (x)
  775.          (let ((meta-specializer 
  776.              (if (eq *boot-state* 'complete)
  777.              (class-of (specializer-class x))
  778.              (class-of x))))
  779.            (cond ((eq x *the-class-t*) t)
  780.              ((*subtypep meta-specializer standard)  'standard-instance)
  781.              ((*subtypep meta-specializer fsc)       'standard-instance)
  782.              ((*subtypep meta-specializer structure) 'structure-instance)
  783.              ((*subtypep meta-specializer built-in)  'built-in-instance)
  784.              ((*subtypep meta-specializer slot)      'slot-instance)
  785.              (t (error "PCL can not handle the specializer ~S (meta-specializer ~S)."
  786.                    new-specializer meta-specializer))))))
  787.       ;;
  788.       ;; We implement the following table.  The notation is
  789.       ;; that X and Y are distinct meta specializer names.
  790.       ;; 
  791.       ;;   NIL    <anything>    ===>  <anything>
  792.       ;;    X      X            ===>      X
  793.       ;;    X      Y            ===>    CLASS
  794.       ;;    
  795.       (let ((new-metatype (specializer->metatype new-specializer)))
  796.     (cond ((eq new-metatype 'slot-instance) 'class)
  797.           ((null metatype) new-metatype)
  798.           ((eq metatype new-metatype) new-metatype)
  799.           (t 'class))))))
  800.  
  801. (defmacro with-dfun-wrappers ((args metatypes)
  802.                   (dfun-wrappers invalid-wrapper-p 
  803.                          &optional wrappers classes types)
  804.                   invalid-arguments-form
  805.                   &body body)
  806.   `(let* ((args-tail ,args) (,invalid-wrapper-p nil) (invalid-arguments-p nil)
  807.       (,dfun-wrappers nil) (dfun-wrappers-tail nil)
  808.       ,@(when wrappers
  809.           `((wrappers-rev nil) (types-rev nil) (classes-rev nil))))
  810.      (dolist (mt ,metatypes)
  811.        (unless args-tail
  812.      (setq invalid-arguments-p t)
  813.      (return nil))
  814.        (let* ((arg (pop args-tail))
  815.           (wrapper nil)
  816.           ,@(when wrappers
  817.           `((class *the-class-t*)
  818.             (type 't))))
  819.      (unless (eq mt 't)
  820.        (setq wrapper (wrapper-of arg))
  821.        (when (invalid-wrapper-p wrapper)
  822.          (setq ,invalid-wrapper-p t)
  823.          (setq wrapper (check-wrapper-validity arg)))
  824.        (cond ((null ,dfun-wrappers)
  825.           (setq ,dfun-wrappers wrapper))
  826.          ((not (consp ,dfun-wrappers))
  827.           (setq dfun-wrappers-tail (list wrapper))
  828.           (setq ,dfun-wrappers (cons ,dfun-wrappers dfun-wrappers-tail)))
  829.          (t
  830.           (let ((new-dfun-wrappers-tail (list wrapper)))
  831.             (setf (cdr dfun-wrappers-tail) new-dfun-wrappers-tail)
  832.             (setf dfun-wrappers-tail new-dfun-wrappers-tail))))
  833.        ,@(when wrappers
  834.            `((setq class (wrapper-class* wrapper))
  835.          (setq type `(class-eq ,class)))))
  836.      ,@(when wrappers
  837.          `((push wrapper wrappers-rev)
  838.            (push class classes-rev)
  839.            (push type types-rev)))))
  840.      (if invalid-arguments-p
  841.      ,invalid-arguments-form
  842.      (let* (,@(when wrappers
  843.             `((,wrappers (nreverse wrappers-rev))
  844.               (,classes (nreverse classes-rev))
  845.               (,types (mapcar #'(lambda (class)
  846.                       `(class-eq ,class))
  847.                           ,classes)))))
  848.        ,@body))))
  849.  
  850.  
  851. ;;;
  852. ;;; Some support stuff for getting a hold of symbols that we need when
  853. ;;; building the discriminator codes.  Its ok for these to be interned
  854. ;;; symbols because we don't capture any user code in the scope in which
  855. ;;; these symbols are bound.
  856. ;;; 
  857.  
  858. (defvar *dfun-arg-symbols* '(.ARG0. .ARG1. .ARG2. .ARG3.))
  859.  
  860. (defun dfun-arg-symbol (arg-number)
  861.   (or (nth arg-number (the list *dfun-arg-symbols*))
  862.       (intern (format nil ".ARG~A." arg-number) *the-pcl-package*)))
  863.  
  864. (defvar *slot-vector-symbols* '(.SLOTS0. .SLOTS1. .SLOTS2. .SLOTS3.))
  865.  
  866. (defun slot-vector-symbol (arg-number)
  867.   (or (nth arg-number (the list *slot-vector-symbols*))
  868.       (intern (format nil ".SLOTS~A." arg-number) *the-pcl-package*)))
  869.  
  870. (defun make-dfun-lambda-list (metatypes applyp)
  871.   (gathering1 (collecting)
  872.     (iterate ((i (interval :from 0))
  873.           (s (list-elements metatypes)))
  874.       (progn s)
  875.       (gather1 (dfun-arg-symbol i)))
  876.     (when applyp
  877.       (gather1 '&rest)
  878.       (gather1 '.dfun-rest-arg.))))
  879.  
  880. (defun make-dlap-lambda-list (metatypes applyp)
  881.   (gathering1 (collecting)
  882.     (iterate ((i (interval :from 0))
  883.           (s (list-elements metatypes)))
  884.       (progn s)
  885.       (gather1 (dfun-arg-symbol i)))
  886.     (when applyp
  887.       (gather1 '&rest))))
  888.  
  889. (defun make-emf-call (metatypes applyp fn-variable &optional emf-type)
  890.   (let ((required
  891.      (gathering1 (collecting)
  892.         (iterate ((i (interval :from 0))
  893.               (s (list-elements metatypes)))
  894.           (progn s)
  895.           (gather1 (dfun-arg-symbol i))))))
  896.     `(,(if (eq emf-type 'fast-method-call)
  897.        'invoke-effective-method-function-fast
  898.        'invoke-effective-method-function)
  899.       ,fn-variable ,applyp ,@required ,@(when applyp `(.dfun-rest-arg.)))))
  900.  
  901. (defun make-dfun-call (metatypes applyp fn-variable)
  902.   (let ((required
  903.       (gathering1 (collecting)
  904.         (iterate ((i (interval :from 0))
  905.               (s (list-elements metatypes)))
  906.           (progn s)
  907.           (gather1 (dfun-arg-symbol i))))))
  908.     (if applyp
  909.     `(function-apply   ,fn-variable ,@required .dfun-rest-arg.)
  910.     `(function-funcall ,fn-variable ,@required))))
  911.  
  912. (defun make-dfun-arg-list (metatypes applyp)
  913.   (let ((required
  914.       (gathering1 (collecting)
  915.         (iterate ((i (interval :from 0))
  916.               (s (list-elements metatypes)))
  917.           (progn s)
  918.           (gather1 (dfun-arg-symbol i))))))
  919.     (if applyp
  920.     `(list* ,@required .dfun-rest-arg.)
  921.     `(list ,@required))))
  922.  
  923. (defun make-fast-method-call-lambda-list (metatypes applyp)
  924.   (gathering1 (collecting)
  925.     (gather1 '.pv-cell.)
  926.     (gather1 '.next-method-call.)
  927.     (iterate ((i (interval :from 0))
  928.           (s (list-elements metatypes)))
  929.       (progn s)
  930.       (gather1 (dfun-arg-symbol i)))
  931.     (when applyp
  932.       (gather1 '.dfun-rest-arg.))))
  933.  
  934.  
  935. ;;;
  936. ;;; Its too bad Common Lisp compilers freak out when you have a defun with
  937. ;;; a lot of LABELS in it.  If I could do that I could make this code much
  938. ;;; easier to read and work with.
  939. ;;;
  940. ;;; Ahh Scheme...
  941. ;;; 
  942. ;;; In the absence of that, the following little macro makes the code that
  943. ;;; follows a little bit more reasonable.  I would like to add that having
  944. ;;; to practically write my own compiler in order to get just this simple
  945. ;;; thing is something of a drag.
  946. ;;;
  947. (eval-when (compile load eval)
  948.  
  949. (defvar *cache* nil)
  950.  
  951. (defconstant *local-cache-functions*
  952.   '((cache () .cache.)
  953.     (nkeys () (cache-nkeys .cache.))
  954.     (line-size () (cache-line-size .cache.))
  955.     (vector () (cache-vector .cache.))
  956.     (valuep () (cache-valuep .cache.))
  957.     (nlines () (cache-nlines .cache.))
  958.     (max-location () (cache-max-location .cache.))
  959.     (limit-fn () (cache-limit-fn .cache.))
  960.     (size () (cache-size .cache.))
  961.     (mask () (cache-mask .cache.))
  962.     (field () (cache-field .cache.))
  963.     (overflow () (cache-overflow .cache.))
  964.  
  965.     ;;
  966.     ;; Return T IFF this cache location is reserved.  The only time
  967.     ;; this is true is for line number 0 of an nkeys=1 cache.  
  968.     ;;
  969.     (line-reserved-p (line)
  970.       (declare (fixnum line))
  971.       (and (= (nkeys) 1)
  972.            (= line 0)))
  973.     ;;
  974.     (location-reserved-p (location)
  975.       (declare (fixnum location))
  976.       (and (= (nkeys) 1)
  977.            (= location 0)))
  978.     ;;
  979.     ;; Given a line number, return the cache location.  This is the
  980.     ;; value that is the second argument to cache-vector-ref.  Basically,
  981.     ;; this deals with the offset of nkeys>1 caches and multiplies
  982.     ;; by line size.  
  983.     ;;       
  984.     (line-location (line)
  985.       (declare (fixnum line))
  986.       (when (line-reserved-p line)
  987.         (error "line is reserved"))
  988.       (if (= (nkeys) 1)
  989.       (the fixnum (* line (line-size)))
  990.       (the fixnum (1+ (the fixnum (* line (line-size)))))))
  991.     ;;
  992.     ;; Given a cache location, return the line.  This is the inverse
  993.     ;; of LINE-LOCATION.
  994.     ;;       
  995.     (location-line (location)
  996.       (declare (fixnum location))
  997.       (if (= (nkeys) 1)
  998.       (floor location (line-size))
  999.       (floor (the fixnum (1- location)) (line-size))))
  1000.     ;;
  1001.     ;; Given a line number, return the wrappers stored at that line.
  1002.     ;; As usual, if nkeys=1, this returns a single value.  Only when
  1003.     ;; nkeys>1 does it return a list.  An error is signalled if the
  1004.     ;; line is reserved.
  1005.     ;;
  1006.     (line-wrappers (line)
  1007.       (declare (fixnum line))
  1008.       (when (line-reserved-p line) (error "Line is reserved."))
  1009.       (location-wrappers (line-location line)))
  1010.     ;;
  1011.     (location-wrappers (location) ; avoid multiplies caused by line-location
  1012.       (declare (fixnum location))
  1013.       (if (= (nkeys) 1)
  1014.       (cache-vector-ref (vector) location)
  1015.       (let ((list (make-list (nkeys)))
  1016.         (vector (vector)))
  1017.         (declare (simple-vector vector))
  1018.         (dotimes (i (nkeys) list)
  1019.           (setf (nth i list) (cache-vector-ref vector (+ location i)))))))
  1020.     ;;
  1021.     ;; Given a line number, return true IFF the line's
  1022.     ;; wrappers are the same as wrappers.
  1023.     ;;
  1024.     (line-matches-wrappers-p (line wrappers)
  1025.       (declare (fixnum line))
  1026.       (and (not (line-reserved-p line))
  1027.            (location-matches-wrappers-p (line-location line) wrappers)))
  1028.     ;;
  1029.     (location-matches-wrappers-p (loc wrappers) ; must not be reserved
  1030.       (declare (fixnum loc))
  1031.       (let ((cache-vector (vector)))
  1032.     (declare (simple-vector cache-vector))
  1033.     (if (= (nkeys) 1)
  1034.         (eq wrappers (cache-vector-ref cache-vector loc))
  1035.         (dotimes (i (nkeys) t)
  1036.           (unless (eq (pop wrappers) (cache-vector-ref cache-vector (+ loc i)))
  1037.         (return nil))))))
  1038.     ;;
  1039.     ;; Given a line number, return the value stored at that line.
  1040.     ;; If valuep is NIL, this returns NIL.  As with line-wrappers,
  1041.     ;; an error is signalled if the line is reserved.
  1042.     ;; 
  1043.     (line-value (line)
  1044.       (declare (fixnum line))
  1045.       (when (line-reserved-p line) (error "Line is reserved."))
  1046.       (location-value (line-location line)))
  1047.     ;;
  1048.     (location-value (loc)
  1049.       (declare (fixnum loc))
  1050.       (and (valuep)
  1051.            (cache-vector-ref (vector) (+ loc (nkeys)))))
  1052.     ;;
  1053.     ;; Given a line number, return true IFF that line has data in
  1054.     ;; it.  The state of the wrappers stored in the line is not
  1055.     ;; checked.  An error is signalled if line is reserved.
  1056.     (line-full-p (line)
  1057.       (when (line-reserved-p line) (error "Line is reserved."))
  1058.       (not (null (cache-vector-ref (vector) (line-location line)))))
  1059.     ;;
  1060.     ;; Given a line number, return true IFF the line is full and
  1061.     ;; there are no invalid wrappers in the line, and the line's
  1062.     ;; wrappers are different from wrappers.
  1063.     ;; An error is signalled if the line is reserved.
  1064.     ;;
  1065.     (line-valid-p (line wrappers)
  1066.       (declare (fixnum line))
  1067.       (when (line-reserved-p line) (error "Line is reserved."))
  1068.       (location-valid-p (line-location line) wrappers))
  1069.     ;;
  1070.     (location-valid-p (loc wrappers)
  1071.       (declare (fixnum loc))
  1072.       (let ((cache-vector (vector))
  1073.         (wrappers-mismatch-p (null wrappers)))
  1074.     (declare (simple-vector cache-vector))
  1075.     (dotimes (i (nkeys) wrappers-mismatch-p)
  1076.       (let ((wrapper (cache-vector-ref cache-vector (+ loc i))))
  1077.         (when (or (null wrapper)
  1078.               (invalid-wrapper-p wrapper))
  1079.           (return nil))
  1080.         (unless (and wrappers
  1081.              (eq wrapper
  1082.                  (if (consp wrappers) (pop wrappers) wrappers)))
  1083.           (setq wrappers-mismatch-p t))))))
  1084.     ;;
  1085.     ;; How many unreserved lines separate line-1 and line-2.
  1086.     ;;
  1087.     (line-separation (line-1 line-2)
  1088.      (declare (fixnum line-1 line-2))
  1089.      (let ((diff (the fixnum (- line-2 line-1))))
  1090.        (declare (fixnum diff))
  1091.        (when (minusp diff)
  1092.      (setq diff (+ diff (nlines)))
  1093.      (when (line-reserved-p 0)
  1094.        (setq diff (1- diff))))
  1095.        diff))
  1096.     ;;
  1097.     ;; Given a cache line, get the next cache line.  This will not
  1098.     ;; return a reserved line.
  1099.     ;; 
  1100.     (next-line (line)
  1101.      (declare (fixnum line))
  1102.      (if (= line (the fixnum (1- (nlines))))
  1103.      (if (line-reserved-p 0) 1 0)
  1104.      (the fixnum (1+ line))))
  1105.     ;;
  1106.     (next-location (loc)
  1107.       (declare (fixnum loc))
  1108.       (if (= loc (max-location))
  1109.       (if (= (nkeys) 1)
  1110.           (line-size)
  1111.           1)
  1112.       (the fixnum (+ loc (line-size)))))
  1113.     ;;
  1114.     ;; Given a line which has a valid entry in it, this will return
  1115.     ;; the primary cache line of the wrappers in that line.  We just
  1116.     ;; call COMPUTE-PRIMARY-CACHE-LOCATION-FROM-LOCATION, this is an
  1117.     ;; easier packaging up of the call to it.
  1118.     ;; 
  1119.     (line-primary (line)
  1120.       (declare (fixnum line))
  1121.       (location-line (line-primary-location line)))
  1122.     ;;
  1123.     (line-primary-location (line)
  1124.      (declare (fixnum line))
  1125.      (compute-primary-cache-location-from-location
  1126.        (cache) (line-location line)))
  1127.     ))
  1128.  
  1129. (defmacro with-local-cache-functions ((cache) &body body)
  1130.   `(let ((.cache. ,cache))
  1131.      (declare (type cache .cache.))
  1132.      (macrolet ,(mapcar #'(lambda (fn)
  1133.                 `(,(car fn) ,(cadr fn)
  1134.                     `(let (,,@(mapcar #'(lambda (var)
  1135.                               ``(,',var ,,var))
  1136.                               (cadr fn)))
  1137.                     ,@',(cddr fn))))
  1138.             *local-cache-functions*)
  1139.        ,@body)))
  1140.  
  1141. )
  1142.  
  1143. ;;;
  1144. ;;; Here is where we actually fill, recache and expand caches.
  1145. ;;;
  1146. ;;; The functions FILL-CACHE and PROBE-CACHE are the ONLY external
  1147. ;;; entrypoints into this code.
  1148. ;;;
  1149. ;;; FILL-CACHE returns 1 value: a new cache
  1150. ;;;
  1151. ;;;   a wrapper field number
  1152. ;;;   a cache
  1153. ;;;   a mask
  1154. ;;;   an absolute cache size (the size of the actual vector)
  1155. ;;; It tries to re-adjust the cache every time it makes a new fill.  The
  1156. ;;; intuition here is that we want uniformity in the number of probes needed to
  1157. ;;; find an entry.  Furthermore, adjusting has the nice property of throwing out
  1158. ;;; any entries that are invalid.
  1159. ;;;
  1160. (defvar *cache-expand-threshold* 1.25)
  1161.  
  1162. (defun fill-cache (cache wrappers value &optional free-cache-p)
  1163.   ;;(declare (values cache))
  1164.   (unless wrappers ; fill-cache won't return if wrappers is nil, might as well check.
  1165.     (error "fill-cache: wrappers arg is NIL!"))
  1166.   (or (fill-cache-p nil cache wrappers value)
  1167.       (and (< (ceiling (* (cache-count cache) 1.25))
  1168.           (if (= (cache-nkeys cache) 1)
  1169.           (1- (cache-nlines cache))
  1170.           (cache-nlines cache)))
  1171.        (adjust-cache cache wrappers value free-cache-p))
  1172.       (expand-cache cache wrappers value free-cache-p)))
  1173.  
  1174. (defvar *check-cache-p* nil)
  1175.  
  1176. (defmacro maybe-check-cache (cache)
  1177.   `(progn
  1178.      (when *check-cache-p*
  1179.        (check-cache ,cache))
  1180.      ,cache))
  1181.  
  1182. (defun check-cache (cache)
  1183.   (with-local-cache-functions (cache)
  1184.     (let ((location (if (= (nkeys) 1) 0 1))
  1185.       (limit (funcall (limit-fn) (nlines))))
  1186.       (dotimes (i (nlines) cache)
  1187.     (when (and (not (location-reserved-p location))
  1188.            (line-full-p i))
  1189.       (let* ((home-loc (compute-primary-cache-location-from-location 
  1190.                 cache location))
  1191.          (home (location-line (if (location-reserved-p home-loc)
  1192.                       (next-location home-loc)
  1193.                       home-loc)))
  1194.          (sep (when home (line-separation home i))))
  1195.         (when (and sep (> sep limit))
  1196.           (error "bad cache ~S ~@
  1197.                       value at location ~D is ~D lines from its home. limit is ~D."
  1198.              cache location sep limit))))
  1199.     (setq location (next-location location))))))
  1200.  
  1201. (defun probe-cache (cache wrappers &optional default limit-fn)
  1202.   ;;(declare (values value))
  1203.   (unless wrappers (error "probe-cache: wrappers arg is NIL!"))
  1204.   (with-local-cache-functions (cache)
  1205.     (let* ((location (compute-primary-cache-location (field) (mask) wrappers))
  1206.        (limit (funcall (or limit-fn (limit-fn)) (nlines))))
  1207.       (declare (fixnum location limit))
  1208.       (when (location-reserved-p location)
  1209.     (setq location (next-location location)))
  1210.       (dotimes (i (1+ limit))
  1211.     (when (location-matches-wrappers-p location wrappers)
  1212.       (return-from probe-cache (or (not (valuep))
  1213.                        (location-value location))))
  1214.     (setq location (next-location location)))
  1215.       (dolist (entry (overflow))
  1216.     (when (equal (car entry) wrappers)
  1217.       (return-from probe-cache (or (not (valuep))
  1218.                        (cdr entry)))))
  1219.       default)))
  1220.  
  1221. (defun map-cache (function cache &optional set-p)
  1222.   (with-local-cache-functions (cache)
  1223.     (let ((set-p (and set-p (valuep))))
  1224.       (dotimes (i (nlines) cache)
  1225.     (unless (or (line-reserved-p i) (not (line-valid-p i nil)))
  1226.       (let ((value (funcall function (line-wrappers i) (line-value i))))
  1227.         (when set-p
  1228.           (setf (cache-vector-ref (vector) (+ (line-location i) (nkeys)))
  1229.             value)))))
  1230.       (dolist (entry (overflow))
  1231.     (let ((value (funcall function (car entry) (cdr entry))))
  1232.       (when set-p
  1233.         (setf (cdr entry) value))))))
  1234.   cache)
  1235.  
  1236. (defun cache-count (cache)
  1237.   (with-local-cache-functions (cache)
  1238.     (let ((count 0))
  1239.       (declare (fixnum count))
  1240.       (dotimes (i (nlines) count)
  1241.     (unless (line-reserved-p i)
  1242.       (when (line-full-p i)
  1243.         (incf count)))))))
  1244.  
  1245. (defun entry-in-cache-p (cache wrappers value)
  1246.   (declare (ignore value))
  1247.   (with-local-cache-functions (cache)
  1248.     (dotimes (i (nlines))
  1249.       (unless (line-reserved-p i)
  1250.     (when (equal (line-wrappers i) wrappers)
  1251.       (return t))))))
  1252.  
  1253. ;;;
  1254. ;;; returns T or NIL
  1255. ;;;
  1256. (defun fill-cache-p (forcep cache wrappers value)
  1257.   (with-local-cache-functions (cache)
  1258.     (let* ((location (compute-primary-cache-location (field) (mask) wrappers))
  1259.        (primary (location-line location)))
  1260.       (declare (fixnum location primary))
  1261.       (multiple-value-bind (free emptyp)
  1262.       (find-free-cache-line primary cache wrappers)
  1263.     (when (or forcep emptyp)
  1264.       (when (not emptyp)
  1265.         (push (cons (line-wrappers free) (line-value free)) 
  1266.           (cache-overflow cache)))
  1267.       ;;(fill-line free wrappers value)
  1268.       (let ((line free))
  1269.         (declare (fixnum line))
  1270.         (when (line-reserved-p line)
  1271.           (error "Attempt to fill a reserved line."))
  1272.         (let ((loc (line-location line))
  1273.           (cache-vector (vector)))
  1274.           (declare (fixnum loc) (simple-vector cache-vector))
  1275.           (cond ((= (nkeys) 1)
  1276.              (setf (cache-vector-ref cache-vector loc) wrappers)
  1277.              (when (valuep)
  1278.                (setf (cache-vector-ref cache-vector (1+ loc)) value)))
  1279.             (t
  1280.              (let ((i 0))
  1281.                (declare (fixnum i))
  1282.                (dolist (w wrappers)
  1283.              (setf (cache-vector-ref cache-vector (+ loc i)) w)
  1284.              (setq i (the fixnum (1+ i)))))
  1285.              (when (valuep)
  1286.                (setf (cache-vector-ref cache-vector (+ loc (nkeys)))
  1287.                  value))))
  1288.           (maybe-check-cache cache))))))))
  1289.  
  1290. (defun fill-cache-from-cache-p (forcep cache from-cache from-line)
  1291.   (declare (fixnum from-line))
  1292.   (with-local-cache-functions (cache)
  1293.     (let ((primary (location-line (compute-primary-cache-location-from-location
  1294.                    cache (line-location from-line) from-cache))))
  1295.       (declare (fixnum primary))
  1296.       (multiple-value-bind (free emptyp)
  1297.       (find-free-cache-line primary cache)
  1298.     (when (or forcep emptyp)
  1299.       (when (not emptyp)
  1300.         (push (cons (line-wrappers free) (line-value free))
  1301.           (cache-overflow cache)))
  1302.       ;;(transfer-line from-cache-vector from-line cache-vector free)
  1303.       (let ((from-cache-vector (cache-vector from-cache))
  1304.         (to-cache-vector (vector))
  1305.         (to-line free))
  1306.         (declare (fixnum to-line))
  1307.         (if (line-reserved-p to-line)
  1308.         (error "transfering something into a reserved cache line.")
  1309.         (let ((from-loc (line-location from-line))
  1310.               (to-loc (line-location to-line)))
  1311.           (declare (fixnum from-loc to-loc))
  1312.           (modify-cache to-cache-vector
  1313.                 (dotimes (i (line-size))
  1314.                   (setf (cache-vector-ref to-cache-vector
  1315.                               (+ to-loc i))
  1316.                     (cache-vector-ref from-cache-vector
  1317.                               (+ from-loc i)))))))
  1318.         (maybe-check-cache cache)))))))
  1319.  
  1320. ;;;
  1321. ;;; Returns NIL or (values <field> <cache-vector>)
  1322. ;;; 
  1323. ;;; This is only called when it isn't possible to put the entry in the cache
  1324. ;;; the easy way.  That is, this function assumes that FILL-CACHE-P has been
  1325. ;;; called as returned NIL.
  1326. ;;;
  1327. ;;; If this returns NIL, it means that it wasn't possible to find a wrapper
  1328. ;;; field for which all of the entries could be put in the cache (within the
  1329. ;;; limit).  
  1330. ;;;
  1331. (defun adjust-cache (cache wrappers value free-old-cache-p)
  1332.   (with-local-cache-functions (cache)
  1333.     (let ((ncache (get-cache-from-cache cache (nlines) (field))))
  1334.       (do ((nfield (cache-field ncache) (next-wrapper-cache-number-index nfield)))
  1335.       ((null nfield) (free-cache ncache) nil)
  1336.     (setf (cache-field ncache) nfield)
  1337.     (labels ((try-one-fill-from-line (line)
  1338.            (fill-cache-from-cache-p nil ncache cache line))
  1339.          (try-one-fill (wrappers value)
  1340.            (fill-cache-p nil ncache wrappers value)))
  1341.       (if (and (dotimes (i (nlines) t)
  1342.              (when (and (null (line-reserved-p i))
  1343.                 (line-valid-p i wrappers))
  1344.                (unless (try-one-fill-from-line i) (return nil))))
  1345.            (dolist (wrappers+value (cache-overflow cache) t)
  1346.              (unless (try-one-fill (car wrappers+value) (cdr wrappers+value))
  1347.                (return nil)))
  1348.            (try-one-fill wrappers value))
  1349.           (progn (when free-old-cache-p (free-cache cache))
  1350.              (return (maybe-check-cache ncache)))
  1351.           (flush-cache-vector-internal (cache-vector ncache))))))))
  1352.  
  1353.                
  1354. ;;;
  1355. ;;; returns: (values <cache>)
  1356. ;;;
  1357. (defun expand-cache (cache wrappers value free-old-cache-p)
  1358.   ;;(declare (values cache))
  1359.   (with-local-cache-functions (cache)
  1360.     (let ((ncache (get-cache-from-cache cache (* (nlines) 2))))
  1361.       (labels ((do-one-fill-from-line (line)
  1362.          (unless (fill-cache-from-cache-p nil ncache cache line)
  1363.            (do-one-fill (line-wrappers line) (line-value line))))
  1364.            (do-one-fill (wrappers value)
  1365.          (setq ncache (or (adjust-cache ncache wrappers value t)
  1366.                   (fill-cache-p t ncache wrappers value))))
  1367.            (try-one-fill (wrappers value)
  1368.          (fill-cache-p nil ncache wrappers value)))
  1369.     (dotimes (i (nlines))
  1370.       (when (and (null (line-reserved-p i))
  1371.              (line-valid-p i wrappers))
  1372.         (do-one-fill-from-line i)))
  1373.     (dolist (wrappers+value (cache-overflow cache))
  1374.       (unless (try-one-fill (car wrappers+value) (cdr wrappers+value))
  1375.         (do-one-fill (car wrappers+value) (cdr wrappers+value))))
  1376.     (unless (try-one-fill wrappers value)
  1377.       (do-one-fill wrappers value))
  1378.     (when free-old-cache-p (free-cache cache))
  1379.     (maybe-check-cache ncache)))))
  1380.  
  1381.  
  1382. ;;;
  1383. ;;; This is the heart of the cache filling mechanism.  It implements the decisions
  1384. ;;; about where entries are placed.
  1385. ;;; 
  1386. ;;; Find a line in the cache at which a new entry can be inserted.
  1387. ;;;
  1388. ;;;   <line>
  1389. ;;;   <empty?>           is <line> in fact empty?
  1390. ;;;
  1391. (defun find-free-cache-line (primary cache &optional wrappers)
  1392.   ;;(declare (values line empty?))
  1393.   (declare (fixnum primary))
  1394.   (with-local-cache-functions (cache)
  1395.     (when (line-reserved-p primary) (setq primary (next-line primary)))
  1396.     (let ((limit (funcall (limit-fn) (nlines)))
  1397.       (wrappedp nil)
  1398.       (lines nil)
  1399.       (p primary) (s primary))
  1400.       (declare (fixnum p s limit))
  1401.       (block find-free
  1402.     (loop
  1403.      ;; Try to find a free line starting at <s>.  <p> is the
  1404.      ;; primary line of the entry we are finding a free
  1405.      ;; line for, it is used to compute the seperations.
  1406.      (do* ((line s (next-line line))
  1407.            (nsep (line-separation p s) (1+ nsep)))
  1408.           (())
  1409.        (declare (fixnum line nsep))
  1410.        (when (null (line-valid-p line wrappers)) ;If this line is empty or
  1411.          (push line lines)        ;invalid, just use it.
  1412.          (return-from find-free))
  1413.        (when (and wrappedp (>= line primary))
  1414.          ;; have gone all the way around the cache, time to quit
  1415.          (return-from find-free-cache-line (values primary nil)))
  1416.        (let ((osep (line-separation (line-primary line) line)))
  1417.          (when (>= osep limit)
  1418.            (return-from find-free-cache-line (values primary nil)))
  1419.          (when (cond ((= nsep limit) t)
  1420.              ((= nsep osep) (zerop (random 2)))
  1421.              ((> nsep osep) t)
  1422.              (t nil))
  1423.            ;; See if we can displace what is in this line so that we
  1424.            ;; can use the line.
  1425.            (when (= line (the fixnum (1- (nlines)))) (setq wrappedp t))
  1426.            (setq p (line-primary line))
  1427.            (setq s (next-line line))
  1428.            (push line lines)
  1429.            (return nil)))
  1430.        (when (= line (the fixnum (1- (nlines)))) (setq wrappedp t)))))
  1431.       ;; Do all the displacing.
  1432.       (loop 
  1433.        (when (null (cdr lines)) (return nil))
  1434.        (let ((dline (pop lines))
  1435.          (line (car lines)))
  1436.      (declare (fixnum dline line))
  1437.      ;;Copy from line to dline (dline is known to be free).
  1438.      (let ((from-loc (line-location line))
  1439.            (to-loc (line-location dline))
  1440.            (cache-vector (vector)))
  1441.        (declare (fixnum from-loc to-loc) (simple-vector cache-vector))
  1442.        (modify-cache cache-vector
  1443.              (dotimes (i (line-size))
  1444.                (setf (cache-vector-ref cache-vector (+ to-loc i))
  1445.                  (cache-vector-ref cache-vector (+ from-loc i)))
  1446.                (setf (cache-vector-ref cache-vector (+ from-loc i))
  1447.                  nil))))))
  1448.       (values (car lines) t))))
  1449.  
  1450. (defun default-limit-fn (nlines)
  1451.   (case nlines
  1452.     ((1 2 4) 1)
  1453.     ((8 16)  4)
  1454.     (otherwise 6)))
  1455.  
  1456. (defvar *empty-cache* (make-cache)) ; for defstruct slot initial value forms
  1457.  
  1458. ;;;
  1459. ;;; pre-allocate generic function caches.  The hope is that this will put
  1460. ;;; them nicely together in memory, and that that may be a win.  Of course
  1461. ;;; the first gc copy will probably blow that out, this really wants to be
  1462. ;;; wrapped in something that declares the area static.
  1463. ;;;
  1464. ;;; This preallocation only creates about 25% more caches than PCL itself
  1465. ;;; uses.  Some ports may want to preallocate some more of these.
  1466. ;;; 
  1467. (eval-when (load)
  1468.   (dolist (n-size '((1 513)(3 257)(3 129)(14 128)(6 65)(2 64)(7 33)(16 32)
  1469.             (16 17)(32 16)(64 9)(64 8)(6 5)(128 4)(35 2)))
  1470.     (let ((n (car n-size))
  1471.       (size (cadr n-size)))
  1472.       (mapcar #'free-cache-vector
  1473.           (mapcar #'get-cache-vector
  1474.               (make-list n :initial-element size))))))
  1475.  
  1476. (defun caches-to-allocate ()
  1477.   (sort (let ((l nil))
  1478.       (maphash #'(lambda (size entry)
  1479.                (push (list (car entry) size) l))
  1480.            pcl::*free-caches*)
  1481.       l)
  1482.     #'> :key #'cadr))
  1483.  
  1484.  
  1485.